Imports System.IO
Imports System.Reflection
Imports System.Collections.Generic
Imports System.Linq
Imports Microsoft.Xna.Framework
Imports Microsoft.Xna.Framework.Audio
Imports Microsoft.Xna.Framework.Content
Imports Microsoft.Xna.Framework.GamerServices
Imports Microsoft.Xna.Framework.Graphics
Imports Microsoft.Xna.Framework.Input
Imports Microsoft.Xna.Framework.Media
Imports Microsoft.Xna.Framework.Input.Touch

Class Gate
	Inherits Tile
    Private Shared tileSequence As New List(Of Sequence)()
	Public switchButton As Integer = 0
	Public elapsedTimeOpen As Single = 0
	Public timeOpen As Single = 6


	Public ReadOnly Property State() As Enumeration.StateTile
		Get
			Return tileState.Value().state
		End Get
	End Property



	Public Sub New(room As RoomNew, Content As ContentManager, tileType As Enumeration.TileType, state As Enumeration.StateTile, switchButton As Integer, NextTileType__1 As Enumeration.TileType, _
		TimeOpen__2 As Single)
		collision = Enumeration.TileCollision.Platform
		MyBase.room = room
		nextTileType = NextTileType__1
		timeOpen = TimeOpen__2

		Me.switchButton = switchButton
		Dim ax As New System.Xml.Serialization.XmlSerializer(tileSequence.[GetType]())
		Dim txtReader As Stream = Microsoft.Xna.Framework.TitleContainer.OpenStream(PrinceOfPersiaGame.CONFIG_PATH_CONTENT & PrinceOfPersiaGame.CONFIG_PATH_SEQUENCES & tileType.ToString().ToUpper() & "_sequence.xml")

		'TextReader txtReader = File.OpenText(PrinceOfPersiaGame.CONFIG_PATH_CONTENT + PrinceOfPersiaGame.CONFIG_PATH_SEQUENCES + tileType.ToString().ToUpper() + "_sequence.xml");

        tileSequence = DirectCast(ax.Deserialize(txtReader), List(Of Sequence))

		For Each s As Sequence In tileSequence
			s.Initialize(Content)
		Next

		If state = Enumeration.StateTile.normal Then
			state = Enumeration.StateTile.closed
		End If

		'Search in the sequence the right type
		Dim result As Sequence = tileSequence.Find(Function(s As Sequence) s.name = state.ToString().ToUpper())

		If result IsNot Nothing Then
			'AMF to be adjust....
			result.frames(0).SetTexture(Content.Load(Of Texture2D)(PrinceOfPersiaGame.CONFIG_TILES & result.frames(0).value))

			collision = result.collision
			Texture = result.frames(0).texture
		End If
		Type = tileType


		'change statetile element
		tileState.Value().state = state
		tileAnimation.PlayAnimation(tileSequence, tileState.Value())
	End Sub

	Public Sub Normal()
		tileState.Value().state = Enumeration.StateTile.normal
		tileAnimation.PlayAnimation(tileSequence, tileState.Value())
	End Sub


	Public Sub Close()
		elapsedTimeOpen = timeOpen
		If tileState.Value().state = Enumeration.StateTile.close Then
			Return
		End If
		If tileState.Value().state = Enumeration.StateTile.closed Then
			Return
		End If

		If tileState.Value().state = Enumeration.StateTile.open Then
			tileState.Add(Enumeration.StateTile.close, Enumeration.PriorityState.Normal, Enumeration.SequenceReverse.Reverse)
		Else
			tileState.Add(Enumeration.StateTile.close)
		End If

		tileAnimation.PlayAnimation(tileSequence, tileState.Value())
	End Sub

	Public Sub Open()
		elapsedTimeOpen = 0
		If tileState.Value().state = Enumeration.StateTile.open Then
			Return
		End If
		If tileState.Value().state = Enumeration.StateTile.opened Then
			Return
		End If
		If tileState.Value().state = Enumeration.StateTile.close Then
			tileState.Add(Enumeration.StateTile.open, Enumeration.PriorityState.Normal, Enumeration.SequenceReverse.FixFrame)
		Else
			tileState.Add(Enumeration.StateTile.open)
		End If

		tileAnimation.PlayAnimation(tileSequence, tileState.Value())
	End Sub

End Class
